<template>
  <div>
    <div align="left">
      <el-button
        size="mini"
        type="success"
        @click="handleAdd()">新增
      </el-button>
    </div>
    <el-table
      :data="tableData"
      border
      style="width: 100%">
      <el-table-column
        prop="id"
        label="ID"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        width="180">
      </el-table-column>
      <el-table-column
        prop="phone"
        label="电话"
        width="180">
      </el-table-column>
      <el-table-column
        prop="email"
        label="邮箱"
        width="180">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="创建时间"
        width="180">
      </el-table-column>
      <el-table-column
        prop="address"
        label="地址">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      align="right"
      layout="prev, pager, next"
      :page-size="pageSize"
      :current-page="currentPage"
      @current-change="getPage"
      :total="totals">
    </el-pagination>

  </div>
</template>

<script>
export default {
  name: 'myTable3',
  data() {
    return {
      pageSize: 5,
      currentPage: 1,
      totals: 0,
      index:-1,
      tableData: []
    }
  },
  methods: {
    getPage(curr) {
      //监听当前页码
      this.currentPage = curr;
      //请求后端接口
      this.queryRequest()
    },
    queryRequest() {//查询请求
      let para = {"currentPage": this.currentPage, "pageSize": this.pageSize}
      //请求后端接口
      this.$http.post("/user/list", para)
        .then(result => {
          //得到后端返回的数据
          this.tableData = result.data.data;
          this.totals = result.data.totals;
        })
        .catch(result => {
          alert("网络不通...")
        })
    },
    handleDelete(index,row){//删除点击事件
      console.log(index,row)
      this.index = index;
      //发送删除请求
      this.deleteResult();
    },
    deleteResult(){//发送删除请求
      this.$http.delete("/user/delete",this.index)
        .then(result=>{
          this.queryRequest();//再次获取数据列表
          if (result.data.success){
            alert("删除成功...")
          }else{
            alert("删除失败...")
          }
        })
        .catch(result=>{
          alert("网络不通...")
        })
    }
  },
  //钩子函数
  mounted() {
    this.queryRequest()
  }
}
</script>


